var tasks = {};
tasks.snapshot = function () {
    threads.start(function () {
        console.log("截图线程启动");
        while (true) {
            var imgPath = "/sdcard/rok_stat/upload/" + new Date().getTime() + ".png";
            images.captureScreen(imgPath);
            var res = http.postMultipart(confModule.getUrl() + "common/upload", {
                file: open(imgPath)
            }, {
                headers: {
                    'token': confModule.getToken()
                }
            });
            files.remove(imgPath)
            var data = confModule.getConfig();
            sleep(data.snapshotInterval);
        }
    });
}
tasks.job = function () {
    threads.start(function () {
        console.log("job线程启动");
        while (true) {
            var data = confModule.getConfig();
            var res = http.get(confModule.getUrl() + "common/command", {
                headers: {
                    'token': confModule.getToken()
                }
            });
            if (res.statusCode == 200) {
                var json = JSON.parse(res.body.string());
                if (json.code == "00000") {
                    for (var i in json.data) {
                        var cmd = json.data[i];
                        var beginExecuteRes = http.postJson(confModule.getUrl() + "common/beginExecute", {"id": cmd.id}, {
                            headers: {
                                'token': confModule.getToken()
                            }
                        });
                        if (beginExecuteRes.statusCode == 200) {
                            var beginExecuteJson = JSON.parse(beginExecuteRes.body.string());
                            if (beginExecuteJson.code == "00000") {
                                if (beginExecuteJson.data > 0) {
                                    console.log("开始执行命令:", cmd.name);
                                    switch (cmd.name) {
                                        case "snapshot":
                                            commandModule.snapshot();
                                            break;
                                        case "extractUnion":
                                            var v = cmd.value;
                                            commandModule.extractUnion(v);
                                            break;
                                        default:
                                            console.log("不支持这个命令");
                                    }
                                    http.postJson(confModule.getUrl() + "common/endExecute", {"id": cmd.id}, {
                                        headers: {
                                            'token': confModule.getToken()
                                        }
                                    });
                                }
                            }
                        }
                        sleep(data.jobCommandInterval);
                    }
                }
            }
            sleep(data.jobCommandInterval);
        }
    });
}
tasks.dkpDaemon = function () {


    while (true) {

        var now = new Date();
        var nowTime = now.getTime();
        var year = now.getFullYear();
        var month = now.getMonth() + 1;//js从0开始取
        var date = now.getDate();
        var deadlineStr = year + "/" + month + "/" + date + " " + "02:00:00";
        var deadline = Date.parse(deadlineStr);
        var deadlineStr2 = year + "/" + month + "/" + date + " " + "06:00:00";
        var deadline2 = Date.parse(deadlineStr2);
        if (nowTime > deadline && nowTime < deadline2) {
            var res = http.get(confModule.getUrl() + "title/getCountByServerId?serverId=" + confModule.getServerId(), {
                headers: {
                    'token': confModule.getToken()
                }
            });
            if (res.statusCode == 200) {
                var json = JSON.parse(res.body.string());
                if (json.code == "00000") {
                    if (json.data == 0) {
                        if (!dkpThread || !dkpThread.isAlive()) {
                            this.dkp();
                        }

                    }
                }
            }
            sleep(300000)
        }
    }
}


var dkpThread;
tasks.dkp = function () {
    if (confModule.getDkpServerId()) {
        confModule.setDkpTaskStatus(1)
        dkpThread = threads.start(function () {
            console.log("dkp线程启动");
            commandModule.dkpExtract();
        })
    }
}


tasks.title = function (titleId) {
    threads.start(function () {
        console.log("title-" + titleId + "线程启动");
        while (true) {
            sleep(5000);
            var data = confModule.getTitleConfig();
            var res = http.get(confModule.getUrl() + "title/getApplyingByServerId?title=" + titleId + "&serverId=" + confModule.getServerId(), {
                headers: {
                    'token': confModule.getToken()
                }
            });
            if (res.statusCode == 200) {
                var json = JSON.parse(res.body.string());
                if (json.code == "00000") {
                    var titleApply = json.data
                    if (titleApply != null) {
                        var serverId = titleApply.serverId;
                        if (titleApply.serverCode != null && titleApply.serverCode != "") {
                            serverId = titleApply.serverCode;
                        }
                        if (dkpThread && dkpThread.isAlive()) {
                            confModule.setDkpTaskStatus(0)
                            while (dkpThread.isAlive()) {
                                sleep(100);
                            }
                        }
                        commandModule.giveTitle(serverId, titleId, titleApply.coordinateX, titleApply.coordinateY)
                        http.get(confModule.getUrl() + "title/updateGetTitleTime?id=" + titleApply.id, {
                            headers: {
                                'token': confModule.getToken()
                            }
                        });
                        switch (titleId) {
                            case 0:
                                sleep(data.dukeKeepTime);
                                break;
                            case 1:
                                sleep(data.scientistKeepTime);
                                break;
                            case 2:
                                sleep(data.architectKeepTime);
                                break;
                        }

                    }
                }
            }


        }
    });
}
module.exports = tasks;